import 'dart:math';

import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/widgets/chart/axis/bar/bar_chart.dart';
import 'package:kq_flutter_core_widget/widgets/chart/kq_chart.dart';
import 'package:get/get.dart';

/// 柱状图
class BarChartPage extends StatelessWidget {
  const BarChartPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('柱状图'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: Get.back,
        ),
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildBarChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildBarChart2(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildBarChart3(),
            ),
          ),
          const SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsets.only(top: 60),
              child: Align(
                alignment: Alignment.center,
                child: Text(
                  "下面为两种无数据的展示方式：",
                  style: TextStyle(fontSize: 20),
                ),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyLineChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyLineChart2(),
            ),
          ),
          const SliverToBoxAdapter(
            child: SizedBox(
              height: 100,
            ),
          ),
        ],
      ),
    );
  }

  Widget buildBarChart() {
    List<BarEntity> entityList = [];
    int xLabelCount = 6;
    double yMin = 0;
    double step = 20;
    int yLabelCount = 6;
    double avg = 0;

    var colors = const [
      Colors.yellow,
      Colors.red,
      Colors.grey,
      Colors.green,
      Colors.deepPurpleAccent,
      Colors.teal,
      Colors.blue
    ];
    for (int i = 0; i < xLabelCount; i++) {
      var value =
          yMin + Random().nextDouble() * (step * (yLabelCount - 1) - yMin);
      BarEntity entity = BarEntity(
          xIndex: i,
          value: value,
          drawLabel: true,
          labelOffset: 4,
          barColors: [colors[i]],
          label: '${value.toInt()}%');
      entityList.add(entity);

      avg += value;
    }

    List<AddedLine> addedLines = [];
    AddedLine addedLine = AddedLine(
        value: avg / entityList.length,
        lineDash: [3, 3],
        label: '测试数据',
        lineWidth: 2,
        lineColor: const Color(0xff5283C9));
    addedLines.add(addedLine);

    BarData barData = BarData(values: entityList, addedLines: addedLines);

    Widget result = SizedBox(
      width: 360,
      height: 240,
      child: KqChart(
        delegate: KqBarChartDelegate.withDefault(
            xAxis: XAxis(
              gridLineWidth: 0,
              gridUseEndPadding: true,
              labelCount: xLabelCount,
              labelMaxLines: 2,
              labelOffsetAxis: 4,
              endPadding: 11,
              labelHorizontalPadding: 10,
              labelRotation: pi / 4,
              axisValueFormatter: (position) =>
                  Label(value: '${position + 1}月'),
            ),
            yAxis: YAxis(
                gridLineWidth: 0.5,
                gridUseEndPadding: true,
                gridLineDash: [2, 2],
                labelMaxLines: 2,
                labelOffsetAxis: 4,
                endPadding: 11,
                labelMaxWidth: 38,
                labelMinWidth: 20,
                labelCount: yLabelCount,
                min: yMin,
                step: step),
            gestureHandler: BarCharGestureHandler(dragEnable: false),
            dataRender: const BarChartDataRender(
                highLightRender: BarChartHighLightRender()),
            data: barData,
            animDuration: const Duration(seconds: 2)),
      ),
    );

    result = Padding(
      padding: const EdgeInsets.only(top: 20),
      child: result,
    );

    return result;
  }

  Widget buildBarChart2() {
    List<BarGroupEntity> groupList = [];
    int xLabelCount = 6;
    double yMin = 0;
    double step = 20;
    int yLabelCount = 6;
    double avg = 0;

    var colors = const [
      Colors.yellow,
      Colors.red,
      Colors.grey,
      Colors.green,
      Colors.deepPurpleAccent,
      Colors.teal,
      Colors.blue
    ];
    for (int i = 0; i < xLabelCount; i++) {
      List<BarEntity> entityList = [];
      for (int j = 0; j < 3; j++) {
        var value =
            yMin + Random().nextDouble() * (step * (yLabelCount - 1) - yMin);
        BarEntity entity = BarEntity(
            xIndex: i,
            value: value,
            labelOffset: 4,
            widthPercent: 0.25,
            barColors: [colors[j]],
            label: '${value.toInt()}%');
        entityList.add(entity);
        avg += value;
      }
      groupList.add(BarGroupEntity(values: entityList));
    }

    List<AddedLine> addedLines = [];
    AddedLine addedLine = AddedLine(
        value: avg / xLabelCount * 3,
        lineDash: [3, 3],
        label: '测试数据',
        lineWidth: 2,
        lineColor: const Color(0xff5283C9));
    addedLines.add(addedLine);

    BarData barData = BarData.group(groups: groupList, addedLines: addedLines);

    Widget result = SizedBox(
      width: 360,
      height: 240,
      child: KqChart(
        delegate: KqBarChartDelegate.withDefault(
            xAxis: XAxis(
              gridLineWidth: 0,
              gridUseEndPadding: true,
              labelCount: xLabelCount,
              labelMaxLines: 2,
              labelOffsetAxis: 4,
              endPadding: 11,
              labelHorizontalPadding: 10,
              axisValueFormatter: (position) =>
                  Label(value: '${position + 1}月'),
            ),
            yAxis: YAxis(
                gridLineWidth: 0.5,
                gridUseEndPadding: true,
                gridLineDash: [2, 2],
                labelMaxLines: 2,
                labelOffsetAxis: 4,
                endPadding: 11,
                labelMaxWidth: 38,
                labelMinWidth: 20,
                labelCount: yLabelCount,
                min: yMin,
                step: step),
            dataRender: const BarChartDataRender(
                highLightRender: BarChartHighLightRender()),
            data: barData,
            animDuration: const Duration(seconds: 2)),
      ),
    );

    result = Padding(
      padding: const EdgeInsets.only(top: 40),
      child: result,
    );

    return result;
  }

  Widget buildBarChart3() {
    List<BarEntity> entityList = [];
    int xLabelCount = 6;
    double yMin = 0;
    double step = 20;
    int yLabelCount = 6;
    double avg = 0;

    var colors = const [
      Colors.yellow,
      Colors.red,
      Colors.grey,
      Colors.green,
      Colors.deepPurpleAccent,
      Colors.teal,
      Colors.blue
    ];
    for (int i = 0; i < xLabelCount; i++) {
      var value =
          yMin + Random().nextDouble() * (step * (yLabelCount - 1) - yMin);
      BarEntity entity = BarEntity(
          xIndex: i,
          value: value,
          labelOffset: 4,
          barColors: [
            colors[2],
            colors[0],
            colors[1],
          ],
          label: '${value.toInt()}%');
      entityList.add(entity);

      avg += value;
    }

    List<AddedLine> addedLines = [];
    AddedLine addedLine = AddedLine(
        value: avg / entityList.length,
        lineDash: [3, 3],
        label: '测试数据',
        lineWidth: 2,
        lineColor: const Color(0xff5283C9));
    addedLines.add(addedLine);

    BarData barData = BarData(values: entityList);

    Widget result = SizedBox(
      width: 360,
      height: 240,
      child: KqChart(
        delegate: KqBarChartDelegate.withDefault(
            xAxis: XAxis(
              gridLineWidth: 0,
              gridUseEndPadding: true,
              labelCount: xLabelCount,
              labelMaxLines: 2,
              labelOffsetAxis: 4,
              endPadding: 11,
              labelHorizontalPadding: 10,
              axisValueFormatter: (position) =>
                  Label(value: '${position + 1}月'),
            ),
            yAxis: YAxis(
                gridLineWidth: 0.5,
                gridUseEndPadding: true,
                gridLineDash: [2, 2],
                labelMaxLines: 2,
                labelOffsetAxis: 4,
                endPadding: 11,
                labelMaxWidth: 38,
                labelMinWidth: 20,
                labelCount: yLabelCount,
                min: yMin,
                step: step),
            dataRender: const BarChartDataRender(
                highLightRender: BarChartHighLightRender()),
            data: barData,
            animDuration: const Duration(seconds: 2)),
      ),
    );

    result = Padding(
      padding: const EdgeInsets.only(top: 40),
      child: result,
    );

    return result;
  }

  Widget buildEmptyLineChart() {
    int xLabelCount = 8;
    double yMin = 0;
    double step = 20;
    int yLabelCount = 6;
    Widget result = SizedBox(
      width: 360,
      height: 240,
      child: KqChart(
        delegate: KqBarChartDelegate(
            xAxis: XAxis(
              gridLineWidth: 0.5,
              gridUseEndPadding: true,
              labelCount: xLabelCount,
              labelMaxLines: 2,
              labelOffsetAxis: 4,
              endPadding: 11,
            ),
            yAxis: YAxis(
                gridLineWidth: 0.5,
                gridUseEndPadding: true,
                labelMaxLines: 2,
                labelOffsetAxis: 4,
                endPadding: 11,
                labelMaxWidth: 38,
                labelMinWidth: 20,
                labelCount: yLabelCount,
                min: yMin,
                step: step),
            animDuration: const Duration(seconds: 2)),
      ),
    );

    result = Padding(
      padding: const EdgeInsets.only(top: 20),
      child: result,
    );

    return result;
  }

  Widget buildEmptyLineChart2() {
    Widget result = Container(
      width: 360,
      height: 240,
      color: Colors.lightBlue[200],
      child: KqChart(
        delegate: KqBarChartDelegate.withDefault(
          xAxis: XAxis(),
          yAxis: YAxis(),
          animDuration: const Duration(seconds: 2),
          emptyWidgetBuilder: () => const Align(
            alignment: Alignment.center,
            child: Text(
              '暂无数据',
              style: TextStyle(fontSize: 12, color: Colors.black),
            ),
          ),
        ),
      ),
    );

    result = Padding(
      padding: const EdgeInsets.only(top: 60),
      child: result,
    );

    return result;
  }
}
